package com.sandwich.admin.service.impl;

import com.sandwich.admin.apibean.CommentRequest;
import com.sandwich.admin.domain.CommentEntity;
import com.sandwich.admin.domain.repository.CommentRepository;
import com.sandwich.admin.service.CommentService;
import com.sandwich.common.Commons;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentRepository commentRepository;

    @Override
    public Page<CommentEntity> queryCommentEntity(CommentRequest commentRequest) {
        PageRequest pageRequest = PageRequest.of(commentRequest.getCurrentPage() - 1, commentRequest.getPageSize());
        return commentRepository.findByStatusAndContentSummaryPkAndParentPk(Commons.ENABLE_STATUS, commentRequest.getContentSummaryPk(), commentRequest.getParentPk(), pageRequest);
    }

    @Override
    public CommentEntity addOrUpdateCommentEntity(CommentEntity commentEntity) {
        return commentRepository.save(commentEntity);
    }
}
